package com.rice.generator;

import cn.hutool.core.io.resource.ClassPathResource;
import com.rice.meta.Meta;
import com.rice.meta.MetaManager;
import freemarker.template.TemplateException;
import com.rice.model.DataModel;

import java.io.File;
import java.io.IOException;

/**
 * 动静结合代码生成
 * @author ricejson
 */
public class MainGenerator {
    public static void doGenerate(DataModel config) throws TemplateException, IOException {
        // 定义原始根路径
        String srcRootPath = "/Users/yupi/Code/yuzi-generator/yuzi-generator-demo-projects/acm-template-pro";
        // 定义原始目标根路径
        String destRootPath = "generated";
        // 动态代码生成
        String srcPath;
        String destPath;
        srcPath = new File(srcRootPath, "src/com/yupi/acm/MainTemplate.java.ftl").getAbsolutePath();
        destPath = new File(destRootPath, "src/com/yupi/acm/MainTemplate.java").getAbsolutePath();
        DynamicGenerator.deGenerator(srcPath, destPath, config);
        // 静态代码生成
        srcPath = new File(srcRootPath, ".gitignore").getAbsolutePath();
        destPath = new File(destRootPath, ".gitignore").getAbsolutePath();
        StaticGenerator.doGenerate(srcPath, destPath);
        // 静态代码生成
        srcPath = new File(srcRootPath, "README.md").getAbsolutePath();
        destPath = new File(destRootPath, "README.md").getAbsolutePath();
        StaticGenerator.doGenerate(srcPath, destPath);
    }

    public static void main(String[] args) throws TemplateException, IOException, InterruptedException {
        // 获取元数据对象
        Meta meta = MetaManager.getMeta();
        // 校验元信息
        ValidateMeta.doValidateMeta(meta);
        // 定义项目路径
        String projectPath = System.getProperty("user.dir");
        // 定义resources路径
        String resourcesPath = new ClassPathResource("").getAbsolutePath();
        // 定义输入路径
        String inputPath = resourcesPath + File.separator + "templates/com/rice/model/DataModel.java.ftl";
        // 定义输出路径
        String outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/model/DataModel.java";
        // 拷贝实例工程到目标文件
        String demoProjectPath = meta.getFileConfig().getDemoProjectPath();
        String copyDestPath = projectPath + File.separator + "generated/.source";
        StaticGenerator.doGenerate(demoProjectPath, copyDestPath);
        // 动态生成数据模型文件
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成命令行工具文件
        inputPath = resourcesPath + File.separator + "templates/com/rice/cli/command/GeneratorCommand.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/cli/command/GeneratorCommand.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成list命令行工具文件
        inputPath = resourcesPath + File.separator + "templates/com/rice/cli/command/ListCommand.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/cli/command/ListCommand.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成config命令行工具文件
        inputPath = resourcesPath + File.separator + "templates/com/rice/cli/command/ConfigCommand.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/cli/command/ConfigCommand.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成命令执行器
        inputPath = resourcesPath + File.separator + "templates/com/rice/cli/CommandExecutor.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/cli/CommandExecutor.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成主生成类
        inputPath = resourcesPath + File.separator + "templates/com/rice/generator/MainGenerator.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/generator/MainGenerator.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成动态生成类
        inputPath = resourcesPath + File.separator + "templates/com/rice/generator/DynamicGenerator.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/generator/DynamicGenerator.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成静态生成类
        inputPath = resourcesPath + File.separator + "templates/com/rice/generator/StaticGenerator.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/generator/StaticGenerator.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成主类
        inputPath = resourcesPath + File.separator + "templates/com/rice/Main.java.ftl";
        outputPath = projectPath + File.separator + "generated/src/main/java/com/rice/Main.java";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态生成pom.xml
        inputPath = resourcesPath + File.separator + "pom.xml.ftl";
        outputPath = projectPath + File.separator + "generated/pom.xml";
        DynamicGenerator.deGenerator(inputPath, outputPath, meta);
        // 动态执行打包逻辑
        JarGenerator.doGenerate(meta.getFileConfig().getOutputRootPath());
    }
}
